package com.boarsoft.boar.oss.util;

import com.boarsoft.bean.ReplyInfo;
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;


/**
 * 桶管理工具类
 *
 * @author tao.wang57
 * @date 2024-03-25 pm
 */
public class BucketManagerUtil {

    private static final Logger log = LoggerFactory.getLogger(BucketManagerUtil.class);

    /**
     * 列举桶
     *
     * @return
     */
    public static ReplyInfo<Object> listBucket(ObsClient obsClient) {
        ReplyInfo<Object> result = new ReplyInfo<Object>();
        List<ObsBucket> buckets = null;
        try {
            // 列举桶
            ListBucketsRequest request = new ListBucketsRequest();
            request.setQueryLocation(true);
            buckets = obsClient.listBuckets(request);
            /*for (ObsBucket bucket : buckets) {
                log.info("ListBuckets successfully");
                log.info("BucketName:" + bucket.getBucketName());
                log.info("CreationDate:" + bucket.getCreationDate());
                log.info("Location:" + bucket.getLocation());
            }*/
        } catch (ObsException e) {
            log.error("ListBuckets failed");
            // 请求失败,打印http状态码
            log.error("HTTP Code: " + e.getResponseCode());
            // 请求失败,打印服务端错误码
            log.error("Error Code:" + e.getErrorCode());
            // 请求失败,打印详细错误信息
            log.error("Error Message:" + e.getErrorMessage());
            // 请求失败,打印请求id
            log.error("Request ID:" + e.getErrorRequestId());
            log.error("Host ID:" + e.getErrorHostId());
        } catch (Exception e) {
            // 其他异常信息打印
            log.error("ListBuckets failed", e);
        }

        result.setData(buckets);
        result.setSuccess(Boolean.TRUE);
        return result;
    }

    /**
     * 创建桶
     * 简单创建
     *
     * @param obsClient
     * @return
     */
    public static ReplyInfo<Object> createSimpleBucket(ObsClient obsClient, String bucketName) {
        try {
            // 创建桶
            ObsBucket bucket = obsClient.createBucket(bucketName);
            log.info("CreateBucket successfully");
            log.info("StatusCode: " + bucket.getStatusCode());
            log.info("RequestId: " + bucket.getRequestId());
        } catch (ObsException e) {
            log.error("CreateBucket failed");
            // 请求失败,打印http状态码
            log.error("HTTP Code: " + e.getResponseCode());
            // 请求失败,打印服务端错误码
            log.error("Error Code:" + e.getErrorCode());
            // 请求失败,打印详细错误信息
            log.error("Error Message: " + e.getErrorMessage());
            // 请求失败,打印请求id
            log.error("Request ID:" + e.getErrorRequestId());
            log.error("Host ID:" + e.getErrorHostId());
        } catch (Exception e) {
            // 其他异常信息打印
            log.error("CreateBucket failed", e);
        }
        return ReplyInfo.SUCCESS;
    }

    /**
     * 创建桶
     * 带参数创建
     *
     * @param obsClient
     * @param location  桶区域位置
     * @return
     */
    public static ReplyInfo<Object> createArgsBucket(ObsClient obsClient, String location, String bucketName) {
        try {
            CreateBucketRequest request = new CreateBucketRequest();
            request.setBucketName(bucketName);
            // 设置桶访问权限为私有读写，默认也是私有读写
            request.setAcl(AccessControlList.REST_CANNED_PRIVATE);
            // 设置桶的存储类别为标准存储
            request.setBucketStorageClass(StorageClassEnum.STANDARD);
            // 设置桶区域位置(以区域为华北-北京四为例)，location 需要与 endpoint的位置信息一致
            request.setLocation(location);
            // 指定创建多AZ桶，如果不设置，默认创建单AZ桶
            request.setAvailableZone(AvailableZoneEnum.MULTI_AZ);
            // 创建桶
            ObsBucket bucket = obsClient.createBucket(request);
            // 创建桶成功
            log.info("CreateBucket successfully");
            log.info("RequestId:" + bucket.getRequestId());
        } catch (ObsException e) {
            log.error("CreateBucket failed");
            // 请求失败,打印http状态码
            log.error("HTTP Code: " + e.getResponseCode());
            // 请求失败,打印服务端错误码
            log.error("Error Code:" + e.getErrorCode());
            // 请求失败,打印详细错误信息
            log.error("Error Message: " + e.getErrorMessage());
            // 请求失败,打印请求id
            log.error("Request ID:" + e.getErrorRequestId());
            log.error("Host ID:" + e.getErrorHostId());
        } catch (Exception e) {
            // 其他异常信息打印
            log.error("CreateBucket failed", e);
        }
        return ReplyInfo.SUCCESS;
    }


    /**
     * 删除桶
     *
     * @param obsClient
     * @param bucketName
     * @return
     */
    public static ReplyInfo<Object> deleteBucket(ObsClient obsClient, String bucketName) {
        try {
            // 删除桶
            HeaderResponse response = obsClient.deleteBucket(bucketName);
            log.info("DeleteBucket successfully");
            log.info("StatusCode:" + response.getStatusCode());
            log.info("RequestId:" + response.getRequestId());
        } catch (ObsException e) {
            log.error("DeleteBucket failed");
            // 请求失败,打印http状态码
            log.error("HTTP Code:" + e.getResponseCode());
            // 请求失败,打印服务端错误码
            log.error("Error Code:" + e.getErrorCode());
            // 请求失败,打印详细错误信息
            log.error("Error Message: " + e.getErrorMessage());
            // 请求失败,打印请求id
            log.error("Request ID:" + e.getErrorRequestId());
            log.error("Host ID:" + e.getErrorHostId());
        } catch (Exception e) {
            // 其他异常信息打印
            log.error("DeleteBucket failed", e);
        }
        return ReplyInfo.SUCCESS;
    }

    /**
     * 判断桶是否存在
     *
     * @return
     */
    public static boolean existsBucket(ObsClient obsClient, String bucketName) {
        boolean exists = Boolean.FALSE;
        try {
            // 判断桶是否存在
            exists = obsClient.headBucket(bucketName);
            log.info("HeadBucket successfully");
            log.info("exists: {}", exists);
        } catch (ObsException e) {
            log.error("HeadBucket failed");
            // 请求失败,打印http状态码
            log.error("HTTP Code:" + e.getResponseCode());
            // 请求失败,打印服务端错误码
            log.error("Error Code :" + e.getErrorCode());
            // 请求失败,打印详细错误信息
            log.error("Error Message:" + e.getErrorMessage());
            // 请求失败,打印请求id
            log.error("Request ID:" + e.getErrorRequestId());
            log.error("Host ID:" + e.getErrorHostId());
        } catch (Exception e) {
            // 其他异常信息打印
            log.error("HeadBucket failed", e);
        }

        return exists;
    }
}
